import router from '@system.router';
import prompt from '@system.prompt';
import socket from 'libsocket.so'
import dataStorage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility';

export default {
    data: {
        inputMsg: '',
        showMore: false,
        addToken:'',
        uid:'',
        moreBtnUrl: '/common/images/more.png',
        MsgList: [{isMe: false,
                      messageType: 0,
                      content:'华为鸿蒙系统是一款全新的面向全场景的分布式操作系统，创造一个超级虚拟终端互联的世界',
                      profPicUrl:'/common/images/bob.jpg',},
                  {isMe: false,
                      messageType: 1,
                      content:'/common/images/harmonyos.jpg',
                      profPicUrl:'/common/images/bob.jpg',}]
    },
    onShow(){
        this.uid =  this.$app.$def.uid;
        if(this.$app.$def.connectStatus==0){
            socket.connect("192.168.199.232",1234);
            this.$app.$def.connectStatus=1;
        }
        if((this.$app.$def.uri.length==1 && this.$app.$def.uri[0]==1) || (this.$app.$def.uri.length==0)){
            this.$app.$def.uri = [];
        }else{

            for (var i = 0; i < this.$app.$def.uri.length; i++) {
                this.MsgList.push({isMe: true,
                    messageType: 1,
                    content: this.$app.$def.uri[i],
                    profPicUrl:'/common/images/my.jpg',});
                this.inputMsg = '';
                this.$element('chatList').scrollBottom(true);
                this.MsgList.push({isMe: false,
                    messageType: 0,
                    content: "你好！",
                    profPicUrl:'/common/images/bob.jpg',});
            }

            this.$app.$def.uri=[];
        }
    },
    inputChange(e){
        this.inputMsg  = e.value;
    },
    onGenerateMessage(message){
        //uid:发送方
        //touid:接收方，不可为空，测试时需要改为测试客户端端口
        //TODO type:say-私聊,group-群发
        this.getToken()
        var obj = { uid: this.uid, touid: 324343 , role:'s' , type:'say', content:message, stype:0,token : this.addToken};
        var json = JSON.stringify(obj);
        console.log(json);
        return json;
    },
    //获取token
    getToken(){
        //取token
        var context = featureAbility.getContext()
        context.getFilesDir().then(v =>{
            dataStorage.getStorage(v + '/smart_build').then(it =>{
                let promise = it.get("token","default")
                promise.then((value) => {
                    console.info("The value of token is " + value)
                    this.addToken = value
                }).catch((err) => {
                    console.info("Get the value of token failed with err: " + err)
                })
            })
        })
    },

    clickSend(e){
        if(this.inputMsg ==''){
            prompt.showToast({message:"输入不能为空"});
            return;
        }
        this.MsgList.push({isMe: true,
            messageType: 0,
            content: this.inputMsg,
            profPicUrl:'/common/images/my.jpg',});
        var obj = this.onGenerateMessage(this.inputMsg);
        var json = JSON.stringify(obj);
        socket.send(json);

        this.inputMsg = '';
        this.$element('chatList').scrollBottom(true);
        this.MsgList.push({isMe: false,
            messageType: 0,
            content: "你好！",
            profPicUrl:'/common/images/bob.jpg',});

    },

    clickMoreBtn(){
        if(!this.showMore){
            this.showMore = true;
            this.moreBtnUrl = '/common/images/fold.png'
        }
        else{
            this.moreBtnUrl = '/common/images/more.png';
            this.showMore = false;
        }
    },

    clickPhoto(){
        router.push({
            uri: "pages/picturedisplay/picturedisplay",
        });
    },

    backToSession(){
        router.back();
    }
}
